/**
 * 
 */
package billing;

import java.io.IOException;
import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

/**
 * Main Class of the Billing-Server
 * 
 * @author Gernot
 * 
 */
public class Server {
	
	private static String registryHost;
	private static int registryPort;

    /**
     * @param args
     */
    public static void main(String[] args) {
        //Properties props = new Properties();

        if (args.length == 1) {

            try {
                /*FileInputStream fis = new FileInputStream("registry.properties");
                props.load(fis);
                fis.close();
                
                int port = Integer.parseInt(props.getProperty("registry.port"));
                String host = props.getProperty("registry.host");*/
            	if (!loadProperties()) {
            		return;
            	}

                // Setup RMI Registry
                Registry registry;
                try {
                    registry = LocateRegistry.createRegistry(registryPort);
                } catch (RemoteException e) {
                    try {
                        registry = LocateRegistry.getRegistry(registryHost, registryPort);
                    } catch (RemoteException e2) {
                        registry = null;
                        System.out.println("RMI Registry location failed: "
                                + e2.getMessage());
                    }
                }
                if (registry != null) {
                    SecureBillingServer secure_srv =  new SecureBillingServer();

                    BillingServerSecure secure_stub = secure_srv;
                    
                    BillingServerImpl billingServer = new BillingServerImpl(
                            secure_stub);
                    
                    registry.bind(args[0], billingServer);
                    
                    System.out.println("Billing Server running, enter \"!exit\" to exit...");
                    //System.in.read();
                    util.Util.waitForExit();
                    
                    System.out.println("Shutting down Server, unexporting & unbinding RMI objects ...");
                    
                    UnicastRemoteObject.unexportObject(secure_srv, true);
                    UnicastRemoteObject.unexportObject(billingServer, true);
                    try {
                        registry.unbind(args[0]);
                    } catch (NotBoundException e) {
                       System.out.println("ERROR:"+e.getMessage());
                       //e.printStackTrace();
                    }
                }

            } catch (IOException e) {
                System.out.println("ERROR:"+e.getMessage());
                //e.printStackTrace();
            } catch (AlreadyBoundException e) {
               System.out.println("ERROR:"+e.getMessage());
               //e.printStackTrace();
            }

        } else {
            System.out.println("Usage: java billing.Server <RmiBindingName>");
        }

    }
	
	
	
	private static boolean loadProperties() {
		boolean ok = false;
		java.io.InputStream is = ClassLoader.getSystemResourceAsStream("registry.properties");
		if (is != null) {
			java.util.Properties props = new java.util.Properties();
			try {
				props.load(is);
				registryHost = props.getProperty("registry.host");
				registryPort = Integer.parseInt(props.getProperty("registry.port"));
				ok = true;
			} catch (IOException e) {
				System.err.println("Could not load registry.properties");
			} catch (NumberFormatException e) {
				System.err.println("registry.port in registry.properties must be integer");
			}
			finally {
				try {
					is.close();
				} catch (IOException e) {
					System.err.println("could not close registry.properties");
				}
			}
		} else {
			System.err.println("Properties file not found!");
		}
		return ok;
	}
}
